// 批改作业提交
function gradeSubmission(submissionId, score, comment) {
    // 设置表单值
    document.getElementById('submissionId').value = submissionId;
    document.getElementById('score').value = score;
    document.getElementById('comment').value = comment;

    // 显示模态框
    const modal = new bootstrap.Modal(document.getElementById('gradeSubmissionModal'));
    modal.show();
}

// 保存批改结果
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('saveGradeBtn').addEventListener('click', function() {
        const submissionId = document.getElementById('submissionId').value;
        const score = document.getElementById('score').value;
        const comment = document.getElementById('comment').value;

        // 发送AJAX请求保存批改结果
        fetch('/admin/assignments/submissions/' + submissionId + '/grade', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: new URLSearchParams({
                'score': score,
                'comment': comment
            })
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('批改结果保存成功');
                    bootstrap.Modal.getInstance(document.getElementById('gradeSubmissionModal')).hide();
                    // 刷新页面以显示更新后的数据
                    location.reload();
                } else {
                    alert('保存失败: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('保存失败，请重试');
            });
    });
});